<template>
  <div class="app-container">
    <div class="menu-container">

      <el-form :inline="true" style="float: right;">
        <el-form-item>
          <el-input v-model="listQuery.nickname" size="mini" placeholder="用户昵称"/>
        </el-form-item>

        <!-- <el-form-item>
          <el-select v-model="listQuery.user_state" placeholder="请选择" size="mini">
            <el-option
              v-for="item in userStateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item> -->

        <el-form-item label="用户来源">
          <el-select v-model="listQuery.user_from" placeholder="请选择" size="mini">
            <el-option
              v-for="item in userFromOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button size="mini" icon="el-icon-search" type="warning" circle @click="getList()"/>
        </el-form-item>

      </el-form>
    </div>
    <el-table v-loading="listLoading" :data="page.records" border fit highlight-current-row style="width: 100%" height="500">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="Open ID">
        <template slot-scope="scope">
          {{ scope.row.openid }}
        </template>
      </el-table-column>

      <el-table-column align="center" label="昵称">
        <template slot-scope="scope">
          {{ scope.row.nickname }}
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="性别">
        <template slot-scope="scope">
          <span>{{ scope.row.gender_desc }}</span>
        </template>
      </el-table-column>

      <el-table-column width="100px" align="center" label="城市">
        <template slot-scope="scope">
          <span>{{ scope.row.city }}</span>
        </template>
      </el-table-column>

      <el-table-column width="100px" align="center" label="头像">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" stype="height:60px" >
        </template>
      </el-table-column>

      <el-table-column width="100px" align="center" label="电话">
        <template slot-scope="scope">
          {{ scope.row.phone }}
        </template>
      </el-table-column>

      <el-table-column width="100px" align="center" label="角色">
        <template slot-scope="scope">
          {{ scope.row.user_state_desc }}
        </template>
      </el-table-column>

      <el-table-column width="100px" align="center" label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="Actions" width="240">
        <template slot-scope="scope">
          <el-button v-if="scope.row.user_from == 1 && scope.row.user_state == 0" type="primary" size="small" @click="modifyUserState(scope.row, 1)">成为客户经理</el-button>
          <el-button v-if="scope.row.user_from == 2 && scope.row.user_state == 0" type="primary" size="small" @click="modifyUserState(scope.row, 3)">成为生产者</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="page.recordsTotal>0" :total="page.recordsTotal" :page.sync="listQuery.pageNum" :page-sizes="[50, 100]" :limit.sync="listQuery.pageSize" @pagination="getList" />

  </div>
</template>

<script>
import { fetchList, updateUser } from '@/api/user'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'SerieList',
  components: { Pagination },
  data() {
    return {
      total: 0,
      listLoading: true,
      listQuery: {
        nickname: '',
        user_state: '',
        user_from: '',
        pageNum: 1,
        pageSize: 100
      },
      userStateOptions: [{ label: '普通用户', value: 0 }, { label: '客户经理', value: 1 }, { label: '团队领导', value: 2 }, { label: '普通生产者', value: 3 }],
      userFromOptions: [{ label: '普通用户', value: 0 }, { label: '客户经理', value: 1 }, { label: '制作方', value: 2 }],
      page: { pageNum: 1, pageSize: 100, pageTotal: 0, recordsTotal: 0, records: [] }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    modifyUserState(user, user_state) {
      updateUser({ id: user.id, user_state: user_state }).then(response => {
        const r = response.data
        if (r.code === 0) {
          this.$message({ message: '状态更新成功', type: 'success' })
          this.getList()
        } else {
          this.$message({ message: r.msg, type: 'error' })
        }
      })
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        const data = response.data
        if (data.code !== 0) {
          this.$message.error(data.msg)
        }
        this.page = data.data
        this.listLoading = false
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
